/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.gateway.handler;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.hyts.infodb.gateway.bean.RouteDefinitionBO;
import com.hyts.infodb.gateway.bean.RouteDefinitionVO;
import com.hyts.infodb.gateway.service.GatewayService;
import com.hyts.infodb.utils.NetworkUtils;
import com.hyts.infodb.context.SpringContextConfiguration;


/** 
 * @author LiBo/Alex
 * @version V1.0 
 */
@Controller
public class GatewayDataHandler {
    
    
    @Value("${server.port}")
    Integer port;
    
    /**  
     * @param serverRequest
     * @return
     * @exception
     */ 
    @PostMapping("/Node/save")
    public String save(RouteDefinitionBO routeDefinitionBO,Model model) {
        GatewayService service = SpringContextConfiguration.getGlobalBean(GatewayService.class);
        service.saveGatewayData(routeDefinitionBO);
        RouteDefinitionVO vo = service.findGatewayDataList(new RouteDefinitionBO());
        model.addAttribute("port",port);
        model.addAttribute("ip", NetworkUtils.getServerIpAddress());
        model.addAttribute("data",vo.getContents());
        return "/Node/index";
    }
    
    
    @GetMapping("/Node/remove/")
    public String remove(@RequestParam("id") String id,Model model) {
        GatewayService service = SpringContextConfiguration.getGlobalBean(GatewayService.class);
        RouteDefinitionBO delete =  new RouteDefinitionBO();
        delete.setId(Long.parseLong(id));
        service.deleteGatewayData(delete);
        RouteDefinitionVO vo = service.findGatewayDataList(new RouteDefinitionBO());
        model.addAttribute("port",port);
        model.addAttribute("ip", NetworkUtils.getServerIpAddress());
        model.addAttribute("data",vo.getContents());
        return "/Node/index";
    }
    
    
    @GetMapping("/Node/query")
    public String query(RouteDefinitionBO routeDefinitionBO,Model model) {
        GatewayService service = SpringContextConfiguration.getGlobalBean(GatewayService.class);
        RouteDefinitionVO vo = service.findGatewayDataList(routeDefinitionBO);
        model.addAttribute("port",port);
        model.addAttribute("ip", NetworkUtils.getServerIpAddress());
        model.addAttribute("data",vo.getContents());
        return "/Node/index";
    }
    
}
